import axios from 'axios'
import ElementUI from 'element-ui';
import store from '../vuex/store' 
// import QS from 'qs';
import router from '../router/index';
import { ElMessage } from './norepeat.js'
let Message = new ElMessage()

axios.defaults.withCredentials = true;  //允许携带cookie

if (process.env.NODE_ENV === 'development') {
	setInterval(function(){
	    axios.defaults.baseURL = localStorage.getItem("requestUrl") 
		// axios.defaults.baseURL = 'http://192.168.0.144:8886' 
	},500)
	
    // axios.defaults.baseURL = 'http://192.168.0.144:8885'     //开发地址用的是vue。config。js里面定义的代理    在本地调试 一般存在跨域问题所以在vue.config.js中定义
} else if (process.env.NODE_ENV === 'production') {
    // var asd = "http://" + window.location.host + "/"
    // axios.defaults.baseURL = asd         //线上请求地址
	setInterval(function(){
		axios.defaults.baseURL = localStorage.getItem("requestUrl") 
	},500)
}


//设置超时15s
axios.defaults.timeout = 15000
setInterval(function(){
    axios.defaults.headers['token'] =  JSON.parse(window.sessionStorage.getItem("token"))
},100)
// 请求头信息是为post请求设置
axios.defaults.headers.post['Content-Type'] = 'application/json;charset=UTF-8'
axios.defaults.headers['token'] =  JSON.parse(sessionStorage.getItem("token"))
axios.interceptors.response.use(
    response => {
        // 如果返回的状态码为200，说明接口请求成功，可以正常拿到数据     
        // 否则的话抛出错误
        if (response.status === 200) {            
            return Promise.resolve(response);      
        } else {            
            // return Promise.reject(response);   
        }
    },
    error => {
        if(error.response.status){
            if(error.response.data.code == "t001"){
                Message.error('登录失效，请重新登录!')
                router.push({path:'/login'})
            }else{
                Message.error(error.response.data.errMsg)
            }
           
            return Promise.reject(error.response)   
        }
    }
);

/** 
 * 请求get方法
 * */ 
export function get(url, params){    
    return new Promise((resolve, reject) =>{
        if(params){
            url+=params;
        }    
        axios.get(url)        
        .then(res => {            
            resolve(res.data);        
        })        
        .catch(err => {            
            // reject(err.data)    
            // console.log(err.data)    
        })    
    });
}

/**
 * 请求post方法
 */

export function post(url, params) {    
    return new Promise((resolve, reject) => {         
        axios.post(url, params)        
        .then(res => {            
            resolve(res.data);        
        })        
        .catch(err => {            
            // reject(err.data)        
        })    
    });
}

// form-data
export function postFormData(url, params) {    
    return new Promise((resolve, reject) => {      
        axios.post(url, params,{headers: {
            'Content-Type': 'multipart/form-data'
            } })       
        .then(res => {            
            resolve(res.data);        
        })        
        .catch(err => {            
            // reject(err.data)        
        })    
    });
}
